package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.adminmanagementbo.*;
import com.cskaoyan.bean.adminmanagementvo.*;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 创建日期: 2022/05/06 09:27
 *
 * @author shuyang
 */
@Service
public class AdminManagementServiceImpl implements AdminManagementService {
    @Autowired
    DashboardMapper dashboardMapper;

    @Override
    public DashboardVO queryTotals() {
        DashboardVO dashboardVO = new DashboardVO();
        dashboardVO.setGoodsTotal(dashboardMapper.selectCountGoodsTotal());
        dashboardVO.setOrderTotal(dashboardMapper.selectCountOrderTotal());
        dashboardVO.setProductTotal(dashboardMapper.selectCountProductTotal());
        dashboardVO.setUserTotal(dashboardMapper.selectCountUserTotal());
        return dashboardVO;
    }

    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Override
    public AddressListVO queryAddress(AddressListBO addressListBO) {
        Integer page = addressListBO.getPage();//页码
        Integer limit = addressListBO.getLimit();//数据量
        String sort = addressListBO.getSort();//排序列
        String order = addressListBO.getOrder();//desc\asc
        String name = addressListBO.getName();
        Integer userId = addressListBO.getUserId();
        PageHelper.startPage(page, limit);
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (userId != null) {
            criteria.andIdEqualTo(userId);
        }
        if (name != null && (!"".equals(name))) {
            name = "%" + name + "%";
            criteria.andNameLike(name);
        }
        marketAddressExample.setOrderByClause(sort + " " + order);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);
        PageInfo<MarketAddress> PageInfo = new PageInfo<>(marketAddresses);
        int total = (int) PageInfo.getTotal();
        int pages = PageInfo.getPages();
        return new AddressListVO(total, pages, limit, page, marketAddresses);
    }

    @Autowired
    MarketCollectMapper marketCollectMapper;

    @Override
    public CollectListVO selectCollects(CollectListBO collectListBO) {
        Integer page = collectListBO.getPage();//页码
        Integer limit = collectListBO.getLimit();//数据量
        String sort = collectListBO.getSort();//排序列
        String order = collectListBO.getOrder();//desc\asc
        Integer valueId = collectListBO.getValueId();
        Integer userId = collectListBO.getUserId();
        PageHelper.startPage(page, limit);
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        marketCollectExample.setOrderByClause(sort + " " + order);
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);
        PageInfo<MarketCollect> marketCollectPageInfo = new PageInfo<>(marketCollects);
        int total = (int) marketCollectPageInfo.getTotal();
        int pages = marketCollectPageInfo.getPages();
        return new CollectListVO(total, pages, limit, page, marketCollects);
    }

    @Autowired
    MarketFootprintMapper marketFootprintMapper;

    @Override
    public FootprintListVO selectFootprints(FootprintListBo footprintListBo) {
        Integer page = footprintListBo.getPage();//页码
        Integer limit = footprintListBo.getLimit();//数据量
        String sort = footprintListBo.getSort();//排序列
        String order = footprintListBo.getOrder();//desc\asc
        Integer goodsId = footprintListBo.getGoodsId();
        Integer userId = footprintListBo.getUserId();
        PageHelper.startPage(page, limit);
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = marketFootprintExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        marketFootprintExample.setOrderByClause(sort + " " + order);
        List<MarketFootprint> marketFootprints = marketFootprintMapper.selectByExample(marketFootprintExample);
        PageInfo<MarketFootprint> marketFootprintPageInfo = new PageInfo<>(marketFootprints);
        int total = (int) marketFootprintPageInfo.getTotal();
        int pages = marketFootprintPageInfo.getPages();
        return new FootprintListVO(total, pages, limit, page, marketFootprints);
    }

    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    @Override
    public HistoryListVO selectHistories(HistoryListBO historyListBO) {
        Integer page = historyListBO.getPage();//页码
        Integer limit = historyListBO.getLimit();//数据量
        String sort = historyListBO.getSort();//排序列
        String order = historyListBO.getOrder();//desc\asc
        String keyword = historyListBO.getKeyword();
        Integer userId = historyListBO.getUserId();
        PageHelper.startPage(page, limit);
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (keyword != null && (!("".equals(keyword)))) {
            keyword = "%" + keyword + "%";
            criteria.andKeywordLike(keyword);
        }
        marketSearchHistoryExample.setOrderByClause(sort + " " + order);
        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
        PageInfo<MarketSearchHistory> marketSearchHistoryPageInfo = new PageInfo<>(marketSearchHistories);
        int pages = marketSearchHistoryPageInfo.getPages();
        int total = (int) marketSearchHistoryPageInfo.getTotal();
        return new HistoryListVO(total, pages, limit, page, marketSearchHistories);
    }

    @Autowired
    MarketFeedbackMapper marketFeedbackMapper;

    @Override
    public FeedbackVO selectFeedbacks(FeedbackBO feedbackBO) {
        Integer page = feedbackBO.getPage();//页码
        Integer limit = feedbackBO.getLimit();//数据量
        String sort = feedbackBO.getSort();//排序列
        String order = feedbackBO.getOrder();//desc\asc
        String username = feedbackBO.getUsername();
        Integer id = feedbackBO.getId();
        PageHelper.startPage(page, limit);
        MarketFeedbackExample marketFeedbackExample = new MarketFeedbackExample();
        MarketFeedbackExample.Criteria criteria = marketFeedbackExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (username != null && (!("".equals(username)))) {
            username = "%" + username + "%";
            criteria.andUsernameLike(username);
        }
        marketFeedbackExample.setOrderByClause(sort + " " + order);
        List<MarketFeedback> marketFeedbacks = marketFeedbackMapper.selectByExample(marketFeedbackExample);
        PageInfo<MarketFeedback> marketFeedbackPageInfo = new PageInfo<>(marketFeedbacks);
        int total = (int) marketFeedbackPageInfo.getTotal();
        int pages = marketFeedbackPageInfo.getPages();
        return new FeedbackVO(total, pages, limit, page, marketFeedbacks);

    }

}
